package codeRandomThoughts.Test647回文子串;

public class Solution {
    public int countSubstrings(String s) {
        int count = 0;
        char[] charS = s.toCharArray();
        //dp[i][j]:字符串s区间范围[i,j](左闭右闭)的子串是否为回文子串
        boolean[][] dp = new boolean[charS.length][charS.length];

        //因为我们求dp值并不全部依赖于之前的值
        //所以初始化 全部为false即可

        //开始递推
        for (int i = charS.length - 1; i >= 0; i--) {
            for (int j = i; j < charS.length; j++) {
                if (charS[i] == charS[j]) {
                    if (j - i <= 1) {
                        //a
                        //aa
                        dp[i][j] = true;
                    } else if (dp[i + 1][j - 1]) {
                        //cabac
                        //要看里面是不是回文子串
                        dp[i][j] = true;
                    }
                }
                if (dp[i][j]) count++;
            }
        }

        return count;
    }
}
